package org.sidora.core;

import java.text.NumberFormat;

/**
 * Classe que representa una actuacio arqueologica
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
public class KeyObject  implements Comparable{
	
	private Site site;
	private Zone zone;
	private Integer number;
	
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	public String getNumberString(){
		
		NumberFormat form = NumberFormat.getInstance();
		form.setMinimumIntegerDigits(3);
		return form.format(number);
		
	}
	
	public Site getSite() {
		return site;
	}
	public void setSite(Site site) {
		this.site = site;
	}
	public Zone getZone() {
		return zone;
	}
	public void setZone(Zone zone) {
		this.zone = zone;
	}
	public String getZoneString(){
		return zone.getIdZone().getNumber().toString();
	}
	@Override
    public boolean equals(Object obj) {
		
    	if (obj == this)	return true;
    	if (!(obj instanceof KeyObject)) return false;
    	KeyObject key = null;	
    	try {
    		key = (KeyObject) obj;
		} catch (Exception e) {
			return false;
		}
		
	boolean num = this.number.equals( key.number);
	boolean sit = this.site.equals (key.site);
	// la zona no estarà sempre present per aixó en cas necessari s'obvia
	boolean zon= true;
	if (this.zone !=null) zon= this.zone.equals(key.zone);
    	if (num && zon && sit) return true;
    	
    	return false; 
    	
    }
	
	public int compareTo(Object obj) {
		KeyObject akey = (KeyObject) obj;
		if (this.zone!=null){
			Zone azone = akey.getZone();
			if (this.zone.compareTo(azone) == 0){
				return this.number.compareTo(akey.number);
			}
			return this.zone.compareTo(azone);
		}else if (this.site!=null){
			Site asite = akey.getSite();
			if (this.site.compareTo(asite) == 0){
				return this.number.compareTo(akey.number);
			}
			return this.site.compareTo(asite);
		}
		return 0;
		
		
	}
	@Override
	public String toString() {
		return number.toString();
	}
	
	
	

}
